package com.sherry.servlet;

import com.alibaba.fastjson.JSONObject;
import com.sherry.model.Order;
import com.sherry.service.IOrderService;
import com.sherry.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet("/order/list")
public class GetOrderServlet extends HttpServlet {
    // 处理post请求
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("接收到前端POST请求");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");


        String orderNo= request.getParameter("orderNo");
        Integer pageIndex= Integer.parseInt(request.getParameter("pageIndex"));

//        获取业务层对象并调用其方法，及传递前端参数给业务层
        IOrderService orderService=new OrderServiceImpl();
        List<Order> orderList =orderService.getOrderList(orderNo,pageIndex);
        Integer totalPage=orderService.getTotalPage(orderNo);
        System.out.println("orderList: " + orderList);

        System.out.println("查询到的订单数据: " + orderList.size());


//        解决了后端返回的Timestamp类型数据在前端无法正常显示的bug
//        让后端返回的时间字段是格式化好的字符串（例如 "2025-06-19 14:30:00"），而不是原始的 Timestamp 或 Date 类型。

//        方案2：Order 类中添加一个额外的字段createTimeStr来保存格式化后的时间字符串

//        方案1：后端返回格式化好的字符串（用 Map 来包装数据）
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 创建一个包装后的 Map 列表
        List<Map<String, Object>> wrappedOrderList = new ArrayList<>();

        for (Order order : orderList) {
            Map<String, Object> orderMap = new HashMap<>();
            orderMap.put("orderNo", order.getOrderNo());

            Timestamp createTime = order.getCreateTime();
            if (createTime != null) {
                String formattedTime = sdf.format(new Date(createTime.getTime()));
                orderMap.put("createTime", formattedTime);
            }
            // 继续添加其他字段
            orderMap.put("companyName", order.getCompanyName());
            orderMap.put("orderPlace", order.getOrderPlace());
            orderMap.put("totalAccount",order.getTotalAccount());
            wrappedOrderList.add(orderMap);
        }


        JSONObject data = new JSONObject();
        data.put("orderList", wrappedOrderList);
        data.put("totalPage", totalPage);
        String dataStr = data.toJSONString();
        PrintWriter writer = response.getWriter();
        writer.write(dataStr);
        writer.flush();
    }

    // 处理get请求
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("接收到前端GET请求");
        doPost(request, response);
    }
}